package com.hmy.contract.manager.entry;

import cn.hutool.json.JSONUtil;
import com.hmy.ccp.common.result.FeignResult;
import com.hmy.ccp.error.exception.HMYRuntimeException;
import com.hmy.fulfillment.api.bo.request.EntryFormBindContractUrlRequest;
import com.hmy.fulfillment.api.bo.request.EntryFormContractSignedSuccessRequest;
import com.hmy.fulfillment.api.bo.response.EntryFormBindContractUrlResponse;
import com.hmy.fulfillment.api.bo.response.EntryFormContractSignedSuccessResponse;
import com.hmy.fulfillment.api.client.EntryFormFeignClient;
import com.hmy.order.api.bo.response.OrderContractInfoUpdateApiResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author seven.qi 齐胜南
 * @version 1.0
 * @description
 * @date 2023/12/28 19:19
 */

@Component
@Slf4j
public class EntryFormFeignClientManager {

    @Resource
    private EntryFormFeignClient entryFormFeignClient;

    /**
     * 更新合同签署链接
     *
     * @param contractInfo 入参
     * @return {@link OrderContractInfoUpdateApiResponse}
     */
    public EntryFormBindContractUrlResponse bindExitFormContractSignUrl(EntryFormBindContractUrlRequest contractInfo) {
        log.info("EntryFormFeignClientManager.bindExitFormContractSignUrl params:{}", JSONUtil.toJsonPrettyStr(contractInfo));
        FeignResult<EntryFormBindContractUrlResponse> response = null;
        response = entryFormFeignClient.bindExitFormContractSignUrl(contractInfo);
        if (Objects.isNull(response)) {
            throw new HMYRuntimeException(502000027);
        }
        if (response.isFailed()) {
            throw new HMYRuntimeException(response.getMsgCode(), response.getErrorText());
        }
        log.info("EntryFormFeignClientManager.bindExitFormContractSignUrl response:{}", JSONUtil.toJsonPrettyStr(response));
        return response.getData();
    }

    /**
     * 合同签约完成回调
     *
     * @param contractInfo 更新合同状态入参
     * @return {@link OrderContractInfoUpdateApiResponse}
     */
    public EntryFormContractSignedSuccessResponse contractSignedSuccess(EntryFormContractSignedSuccessRequest contractInfo) {
        log.info("EntryFormFeignClientManager.contractSignedSuccess params:{}", JSONUtil.toJsonPrettyStr(contractInfo));
        FeignResult<EntryFormContractSignedSuccessResponse> response = null;
        response = entryFormFeignClient.contractSignedSuccess(contractInfo);
        if (Objects.isNull(response)) {
            throw new HMYRuntimeException(502000026);
        }
        if (response.isFailed()) {
            throw new HMYRuntimeException(response.getMsgCode(), response.getErrorText());
        }
        log.info("EntryFormFeignClientManager.contractSignedSuccess response:{}", JSONUtil.toJsonPrettyStr(response));
        return response.getData();
    }
}
